﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using FinApp.ViewModels.Base;
using FinApp.Core;

namespace FinApp.ViewModels.Repositories {
    public class ViewModelFactory : Singleton<ViewModelFactory> {

        #region Members
        private Dictionary<Type, object> instancesDictionary;
        #endregion

        #region Constructors
        public ViewModelFactory() {
            this.instancesDictionary = new Dictionary<Type, object>();
        }
        #endregion

        #region Properties
        #endregion

        #region Methods
        /// <summary>
        /// Obtiene una instancia del tipo de ViewModel concreto
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="isReusable"></param>
        /// <returns></returns>
        public T GetViewModel<T>(bool isReusable = true) where T : ViewModelBase {
            Type currentType = typeof(T);
            if (isReusable) {
                if (this.instancesDictionary.ContainsKey(typeof(T))) {
                    return this.instancesDictionary[currentType] as T;
                } else {
                    object instance = Activator.CreateInstance<T>();
                    this.instancesDictionary.Add(currentType, instance);
                    return instance as T;
                }
            } else {
                return Activator.CreateInstance<T>() as T;
            }
        }
        #endregion

    }
}
